// pages/problem/problem.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceCateId:0,
    list:[],
    num:0,
    problemList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var index = wx.getStorageSync("son");
   
    if (index == "") {
      that.setData({
        serviceCateId: wx.getStorageSync("sons")[0].id
      })
    } else {
      that.setData({
        serviceCateId: wx.getStorageSync("sons")[index].id
      })
    }
    wx.request({
      url: 'https://www.xiaozheng8.com/computePrice/compute',
      method:"post",
      data:{
        cateId:that.data.serviceCateId
      },
      success:function(res){
        console.log(res.data.data.items);
        that.setData({
          list:res.data.data.items
        })
        for(var i=0;i<that.data.list.length;i++){
          that.data.list[i].cateId=0
        };
        that.setData({
          list:that.data.list
        });
        console.log(that.data.list)
        if (wx.getStorageSync("problemList") == "" || wx.getStorageSync("problemList")==null) {

        } else {
          that.setData({
            list: wx.getStorageSync("problemList")
          })
       

      
        }
      }

    })
    
  },
problem:function(e){
  var index = e.currentTarget.dataset.select;
var that=this;
  if (this.data.list[index].cateId == 1){
    that.data.list[index].cateId = 0
  }else{
    that.data.list[index].cateId = 1
  }

  this.setData({
    num: e.currentTarget.dataset.select,
    list: that.data.list
  });
  console.log(that.data.list)
},
  submit:function(){
    var that=this;
    var problem=[];
    wx.setStorageSync("problemList", that.data.list)
    for(var i=0;i<this.data.list.length;i++){
      if (this.data.list[i].cateId==1){
        problem.push(that.data.list[i]);
      }
      console.log(problem);
    }
    if(problem==[]){
      wx.showToast({

        title: '请填问题描述',

        duration: 2000,

        icon: "none"

      })
    }else{
      wx.setStorageSync("problem", problem);

      wx.navigateBack({
      });
    }
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})